//表格初始化
$(document).ready(function() {
    $('#tbodydata2').DataTable( {
        // fixedHeader: true
    } );
} );
//查询方法
let optionJson = [];
function refurbishIndex(){
    // $("#resultcoll  tr").html("");
    let start_time = $("input[name='start_time']").val();
    let end_time = $("input[name='end_time']").val();
    let keyword = $("input[name='keyword']").val();
    if(start_time == 0){
        start_time = "2017-05-02"
    }
    if(end_time ==0){
        end_time = "2017-06-31"
    }
    if(keyword ==0){
        keyword = "13010071406"
    }
    let request_parameters = {type:"get_message_complete","startTime":start_time,"endTime":end_time,"phoneNum":keyword};
    $.ajax({
        type:"post",
        url:"http://127.0.0.1:9998/show/get_message_record_complete",
        data:JSON.stringify(request_parameters),
        contentType: "application/json;charset=utf-8",
        async: false,
        success:function (call_record_complete_info) {
            //   console.log(call_record_complete_info);
            optionJson=call_record_complete_info;
            $('#tbodydata2').dataTable().fnDestroy();//还原初始化
            $('#tbodydata2').DataTable({
                // "serverSide":true,
                //  "paging":true,
                // "lengthMenu":[15,25,35,100,25],
                // "pagingType":"scrolling",
                // serverSide:true,
                // Processing:true,
                // deferRender:true,
                "bStateSave":true,//翻页时保存状态
                // iDisplayLength:10,
                // iDisplayStart:0,
                "ordering":false,//禁用全局排序
                "sort":"position",
                data:optionJson.data,
                //设置列数据
                columns:[
                    {"data": 'sendNum',
                        "render":function (data,type,row,meta) {
                            return data = '<p style="text-align: center">'+data+'</p>'
                        }
                    },
                    {"data": 'recNum',
                        "render":function (data,type,row,meta) {
                            return data = '<p style="text-align: center">'+data+'</p>'
                        }
                    },
                    {"data": 'time',
                        "render":function (data,type,row,meta) {
                            return data = '<p style="text-align: center">'+data+'</p>'
                        }
                    },
                    {"data": 'content',},
                    {"data": 'city',
                        "render":function (data,type,row,meta) {
                            return data = '<p style="text-align: center">'+data+'</p>'
                        }
                    }
                ],
                //设置排序
                "order":[[1,'asc']],
                //设置语言
                language: {
                    "sProcessing": "处理中...",
                    "sLengthMenu": "显示 _MENU_ 项搜索结果",
                    "sZeroRecords": "没有匹配结果",
                    "sInfo": "显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 条数据",
                    "sInfoEmpty": "显示第 0 至 0 项结果，共 0 项",
                    "sInfoFiltered": "(由 _MAX_ 项结果过滤)",
                    "sInfoPostFix": "",
                    "sSearch": "搜索:",
                    "sUrl": "",
                    "sEmptyTable": "未搜索到数据",
                    "sLoadingRecords": "载入中...",
                    "sInfoThousands": ",",
                    "oPaginate": {
                        "sFirst": "首页",
                        "sPrevious": "上页",
                        "sNext": "下页",
                        "sLast": "末页"
                    },
                    "oAria": {
                        "sSortAscending": ": 以升序排列此列",
                        "sSortDescending": ": 以降序排列此列"
                    }
                },
            });
            // let str = "";
            // //  let len = optionJson.length;
            // let j=1;
            // for(let i in optionJson.data) {
            //     str += "<tr  style='text-align: center'>" +
            //         "<th style='text-align: center'>" + j++ + "</th>" +
            //         "<th style='text-align: center'>" + optionJson.data[i].send_num + "</th>" +
            //         "<th style='text-align: center'>" + optionJson.data[i].rec_num + "</th>" +
            //         "<th style='text-align: center'>" + optionJson.data[i].time + "</th>" +
            //         "<th>" + optionJson.data[i].content+ "</th>" +
            //         "<th style='text-align: center'>" + optionJson.data[i].city + "</th>" +
            //         "</tr>";
            //     document.getElementById("tbodydata2").innerHTML=str;
            // }
            // document.getElementById("sum").innerHTML=len;
        }
    });
}
function Message_Excelexport() {

            const jsonData =optionJson.data;
            //列标题，逗号隔开，每一个逗号就是隔开一个单元格
            let str = `发信号码,收信号码,短信发送时间,短信内容,所在城市\n`;
            //增加\t为了不让表格显示科学计数法或者其他格式
            for(let i in jsonData ){
                for(let item in jsonData[i]){
                    str+=`${jsonData[i][item] + '\t'},`;
                }
                str+='\n';
            }
            //encodeURIComponent解决中文乱码
            let uri = 'data:text/csv;charset=utf-8,\ufeff' + encodeURIComponent(str);
            //通过创建a标签实现
            let link = document.createElement("a");
            link.href = uri;
            //对下载的文件命名
            link.download =  "用户短信记录表.xls";
            document.body.appendChild(link);
            link.click();
            document.body.removeChild(link);
        }
